﻿using Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace Web.Hanlder
{
    /// <summary>
    /// UserHanlder 的摘要说明
    /// </summary>
    public class UserHanlder : IHttpHandler
    {
        #region Base
        HttpRequest Request;
        HttpResponse Response;
        System.Web.SessionState.HttpSessionState Session;
        HttpServerUtility Server;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
            Session = context.Session;
            Server = context.Server;
            string method = Request["Method"].ToString();
            System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(method);
            methodInfo.Invoke(this, null);
        }
      
        public void Login()
        {
            string Account = Request["Account"];
            string Password = Request["Password"];
            string Role = Request["Role"];
            int rid = Convert.ToInt32(Role);
            if (rid<=0)
            {
                 string result = JsonConvert.SerializeObject(false);
                Response.Write(result);
                Response.End();
            }
            var model = GetOBJList().FirstOrDefault(c => c.account.Equals(Account) && c.password.Equals(Password)&&c.roleid.Equals(rid));
            if (model == null)
            {
                string result = JsonConvert.SerializeObject(false);
                Response.Write(result);
                Response.End();

            }
            else
            {
                Common.UICookie.AddCookie("UId", model.Id.ToString());
    
                string result = JsonConvert.SerializeObject(true);
                Response.Write(result);
                Response.End();
            }
        }
        public void Exit() {
            Common.ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            if (string.IsNullOrEmpty(Common.UICookie.ReadCookie("ClientUId")))
            {
                rh.states = 1;
                rh.msg = "登录过期！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            else
            {
                Common.UICookie.removeCookie("ClientUId");
                rh.states = 0;
                rh.msg = "操作成功！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
           

        }
        public void ChangePwd()
        {
           
            Common.ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            if (string.IsNullOrEmpty(Common.UICookie.ReadCookie("ClientUId")))
            {
                rh.states = 1;
                rh.msg = "登录过期！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            int Id = Convert.ToInt32(Common.UICookie.ReadCookie("ClientUId"));
            string oldpwd = Request["oldpwd"];
            string newpwd = Request["newpwd"];
            var truepassword = Request["truepassword"];
            if (truepassword.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "确认密码验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            if (!truepassword.Equals(newpwd))
            {
                rh.states = 1;
                rh.msg = "两次密码不一致！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var model = GetOBJList().FirstOrDefault(c => c.Id.Equals(Id));
            if (model!=null)
            {
                if (model.password.Equals(oldpwd))
                {
                    model.password = newpwd;
                    Common.SqlExption.Update(model);
                    rh.states = 0;
                    rh.msg = "操作成功！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
                else
                {
                    rh.states = 1;
                    rh.msg = "密码验证失败！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
        }

        public void ChangePWD() {
            Common.ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            if (string.IsNullOrEmpty(Common.UICookie.ReadCookie("UId")))
            {
                rh.states = 1;
                rh.msg = "登录过期！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            string oldpwd = Request["oldpwd"];
            string newpwd = Request["newpwd"];
            int Id = Convert.ToInt32(Common.UICookie.ReadCookie("UId"));
            var model = GetOBJList().FirstOrDefault(c => c.Id.Equals(Id));
            if (model != null)
            {
                if (model.password.Equals(oldpwd))
                {
                    model.password = newpwd;
                    Common.SqlExption.Update(model);
                    rh.states = 0;
                    rh.msg = "操作成功！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
                else
                {
                    rh.states = 1;
                    rh.msg = "密码验证失败！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
        }

        public void ClientLogin()
        {
            Common.ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            string Account = Request["Account"];
            string Password = Request["Password"];
            string Role = Request["Role"];
            int rid = Convert.ToInt32(Role);
            if (rid <= 0)
            {
                string result = JsonConvert.SerializeObject(false);
                Response.Write(result);
                Response.End();
            }
            var model = GetOBJList().FirstOrDefault(c => c.account.Equals(Account) && c.password.Equals(Password) && c.roleid.Equals(rid));
            if (model == null)
            {
                rh.states = 1;
                rh.msg = "账号或者密码错误！";
                Response.Write(JsonConvert.SerializeObject(rh));
                Response.End();

            }
            else
            {
                rh.states = 0;
                rh.msg = model.name;
                Common.UICookie.AddCookie("ClientUId", model.Id.ToString());
                
               
                Response.Write(JsonConvert.SerializeObject(rh));
                Response.End();
            }
        }

        public List<Model.tb_user> GetOBJList()
        {
            return Common.SqlExption.GetList<Model.tb_user>();
        }
        public void GetList()
        {
            var data = Common.SqlExption.GetList<Model.tb_user>().AsQueryable();
            if (Request["roleid"] != null)
            {
                int roleid = 0;
                if (int.TryParse(Request["roleid"],out roleid))
                {
                    if (roleid > 0)
                    {
                        data = data.Where(c => c.roleid.Equals(roleid));
                    }
                    else
                    {
                        data = data.Where(c => !c.roleid.Equals(6));
                    }
                }
                
              
               
            }
            if (Request["password"] != null)
            {
                string txt_search = Request["password"];
                data = data.Where(c => c.password.Contains(txt_search));
            }
            if (Request["name"] != null)
            {
                string txt_search = Request["name"];
                data = data.Where(c => c.name.Contains(txt_search));
            }
            if (Request["user_number"] != null)
            {
                string txt_search = Request["user_number"];
                data = data.Where(c => c.user_number.Contains(txt_search));
            }
            if (Request["Address"] != null)
            {
                string txt_search = Request["Address"];
                data = data.Where(c => c.Address.Contains(txt_search));
            }
            if (Request["Tel"] != null)
            {
                string txt_search = Request["Tel"];
                data = data.Where(c => c.Tel.Contains(txt_search));
            }
            if (Request["head_img"] != null)
            {
                string txt_search = Request["head_img"];
                data = data.Where(c => c.head_img.Contains(txt_search));
            }
            string result = JsonConvert.SerializeObject(data.ToList());
            Response.Write(result);
            Response.End();

        }
        public void Del()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = int.Parse(Request["Id"]);

            var model = GetOBJList().FirstOrDefault(c => c.Id.Equals(Id));
            if (model != null)
            {
                Common.SqlExption.Delete<Model.tb_user>(Id);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();
        }
        public void Edit()
        {


            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = Convert.ToInt32(Request["Id"]);
            if (!int.TryParse(Request["Id"], out Id))
            {
                if (Id < 0)
                {
                    rh.states = 1;
                    rh.msg = " 验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
            var account = Request["account"];
            if (account.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "账号验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var password = Request["password"];
            if (password.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "密码验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            int roleid =0;
            if (!int.TryParse(Request["roleid"], out roleid))
            {
                if (roleid <= 0)
                {
                    rh.states = 1;
                    rh.msg = "权限验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
          
            var name = Request["name"];
            if (name.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "姓名验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var user_number = Request["user_number"];
            if (user_number.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "编号验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var Address = Request["Address"];
            if (Address.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "住址验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var Tel = Request["Tel"];
            if (Tel.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "联系电话验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            //var head_img = Request["head_img"];
            //if (head_img.Trim().Length <= 0)
            //{
            //    rh.states = 1;
            //    rh.msg = "头像验证错误！";
            //    string json = JsonConvert.SerializeObject(rh);
            //    Response.Write(json);
            //    Response.End();
            //}
            var fs = Request.Files["fileUp"];
            string head_img = string.Empty;
            if (fs != null)
            {
                if (fs.ContentLength <= 0)
                {
                    return;
                }
                string fileDianHua = Path.GetFileName(fs.FileName);
                string fileExt = Path.GetExtension(fileDianHua);
                string imageDir = "/ImageFile/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
                Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(imageDir)));//创建存储图片的目录;
                string newFileDianHua = DateTime.Now.ToString("yyyyMMddHHmmss");//使用GUID作为文件的新名称，当然也可以使用其他的方式对文件进行重命名。
                string fullDir = imageDir + newFileDianHua + fileExt;//完整的图片存储的路径
                string p = Server.MapPath(fullDir);

                head_img = fullDir;
                fs.SaveAs(p);
            }
            var model = Common.SqlExption.GetList<Model.tb_user>().FirstOrDefault(c => c.Id.Equals(Id));
            if (model == null)
            {
                model = new Model.tb_user();
                model.Id = Id;
                model.account = account;
                model.password = password;
                model.roleid = roleid;
                model.name = account;
                model.user_number = user_number;
                model.Address = Address;
                model.Tel = Tel;
                if (!string.IsNullOrEmpty(head_img))
                {
                    model.head_img = head_img;
                }

                model.brothday = DateTime.Now;
                Common.SqlExption.Add(model);
            }
            else
            {
                DateTime brothday = DateTime.Now;
                if (!DateTime.TryParse(Request["brothday"], out brothday))
                {

                    rh.states = 1;
                    rh.msg = "生日验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();

                }
                model.Id = Id;
                model.account = account;
                model.password = password;
                model.roleid = roleid;
                model.name = name;
                model.user_number = user_number;
                model.Address = Address;
                model.Tel = Tel;
                if (!string.IsNullOrEmpty(head_img))
                {
                    model.head_img = head_img;
                }

                model.brothday = brothday;
                Common.SqlExption.Update(model);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();

        }
        public void Reg()
        {


            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = Convert.ToInt32(Request["Id"]);
            if (!int.TryParse(Request["Id"], out Id))
            {
                if (Id < 0)
                {
                    rh.states = 1;
                    rh.msg = " 验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }
            var account = Request["account"];
            if (account.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "账号验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var password = Request["password"];
            if (password.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "密码验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var truepassword = Request["truepassword"];
            if (truepassword.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "确认密码验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            if (!truepassword.Equals(password))
            {
                 rh.states = 1;
                rh.msg = "两次密码不一致！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            int roleid = 0;
            if (!int.TryParse(Request["roleid"], out roleid))
            {
                if (roleid <= 0)
                {
                    rh.states = 1;
                    rh.msg = "权限验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                }
            }

            var name = Request["name"];
            if (name.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "姓名验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var user_number = Request["user_number"];
            if (user_number.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "编号验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var Address = Request["Address"];
            if (Address.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "住址验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var Tel = Request["Tel"];
            if (Tel.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "联系电话验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            var head_img = Request["head_img"];
            if (head_img.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "头像验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }

            var model = Common.SqlExption.GetList<Model.tb_user>().FirstOrDefault(c => c.Id.Equals(Id));
            if (model == null)
            {
                model = new Model.tb_user();
                model.Id = Id;
                model.account = account;
                model.password = password;
                model.roleid = roleid;
                model.name = account;
                model.user_number = user_number;
                model.Address = Address;
                model.Tel = Tel;
                model.head_img = head_img;
                model.brothday = DateTime.Now;
                Common.SqlExption.Add(model);
            }
            else
            {
                DateTime brothday = DateTime.Now;
                if (!DateTime.TryParse(Request["brothday"], out brothday))
                {

                    rh.states = 1;
                    rh.msg = "生日验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();

                }
                model.Id = Id;
                model.account = account;
                model.password = password;
                model.roleid = roleid;
                model.name = name;
                model.user_number = user_number;
                model.Address = Address;
                model.Tel = Tel;
                model.head_img = head_img;
                model.brothday = brothday;
                Common.SqlExption.Update(model);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();

        }
        public void SeverExit() {
            Common.ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            if (string.IsNullOrEmpty(Common.UICookie.ReadCookie("UId")))
            {
                rh.states = 1;
                rh.msg = "登录过期！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
            else
            {
                Common.UICookie.removeCookie("UId");
                rh.states = 0;
                rh.msg = "操作成功！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
            }
           
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #endregion
    }
}